package com.hdjk.view.network;

import com.hdjk.view.callback.BuyGroupCallback;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.callback.GetBuyGroupFeeCallBack;
import com.hdjk.view.callback.GetPerformanceChartCallBack;
import com.hdjk.view.callback.GetRecommendGroupCallBack;
import com.hdjk.view.callback.GroupDetailCallBack;
import com.hdjk.view.config.Constant;
import com.lst.projectlib.entity.Msg;
import com.zhy.http.okhttp.OkHttpUtils;

import okhttp3.Call;

/**
 * Created by sxy on 2017/11/17.
 */

public class GroupNetWork {
    /**
     * 获取推荐组合
     * @param token
     * @param jsonInfo
     * @param listener
     */
    public static void getRecommendGroup(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_GET_RECOMMEND_GROUP)
                    .addParams("assess", jsonInfo)
                    .addHeader("Authorization","Bearer "+token)
                    .build()
                    .execute(new GetRecommendGroupCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }
    /**
     * 获取组合详情
     * @param token
     * @param jsonInfo
     * @param listener
     */
    public static void getGroupDetail(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_GET_GROUP_DETAIL)
                    .addParams("paramList", jsonInfo)
                    .addHeader("Authorization","Bearer "+token)
                    .build()
                    .execute(new GroupDetailCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    public static void getPortfolioPerfromanceReference(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_GET_PORTFPLIO_PERFORMANCE_REFERENCE)
                    .addParams("paramList", jsonInfo)
                    .addHeader("Authorization","Bearer "+token)
                    .build()
                    .execute(new GetPerformanceChartCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    public static void getPortfolioPerfromance(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_GET_PORTFPLIO_PERFORMANCE)
                    .addParams("paramList", jsonInfo)
                    .addHeader("Authorization","Bearer "+token)
                    .build()
                    .execute(new GetPerformanceChartCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 获取购买组合费率和估算费用
     * @param jsonInfo
     * @param listener
     */
    public static void getBuyGroupFee(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_GET_BUY_GROUP_FEE)
                    .addHeader("Authorization","Bearer "+token)
                    .addParams("paramList", jsonInfo)
                    .build()
                    .execute(new GetBuyGroupFeeCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 购买组合
     * @param jsonInfo
     * @param listener
     */
    public static void buyGroup(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.post().url(Constant.URL_BUY_GROUP+"&paramList="+jsonInfo)
                    .addHeader("Authorization","Bearer "+token)
                    .build()
                    .execute(new BuyGroupCallback() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }
}
